package com.winchain.chainbill.bill.biz.repurchase.rx;

import com.shell.bee.base.utils.DateUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.RpurApplyKeyword;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRpurSuccessMsgSubscriber extends OnErrorNotImplementedSubscriber<BillEvent<CbRpur>> {
	@Autowired
	CbDctService cbDctService;

	public void onApplicationEvent(BillEvent<CbRpur> billEvent) {
		CbRec cbRec = billEvent.getBill().getRec();
		CbRpur cbRpur = (CbRpur) billEvent.getBill().getSource();
		RpurApplyKeyword applyKeyword = new RpurApplyKeyword();
		applyKeyword.setBillAmt(String.valueOf(cbRpur.getRpurAmt()));
		applyKeyword.setRpurDate(DateUtil.parseDate(cbRpur.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
		applyKeyword.setBillCode(cbRec.getBillCode());
		applyKeyword.setBuyerNm(cbRpur.getBuyerNm());
		CbDct data = (CbDct) this.cbDctService.selectByKey(cbRpur.getDctId());
		String buyerId = data.getFinId();

		MessageSender.sendMessageByTmpl(CommonDataUtil.getUserId(), "MT02615", applyKeyword, buyerId);
	}
}
